package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Controller
public class ImageController {

    /**
     * 自定义图片上传
     * @param file : 图片路径,注意参数名要一致
     * @return
     */
    @RequestMapping("/images")
    @ResponseBody
    public Map upload(MultipartFile file){
        Map<String,Object> map=new HashMap<>();
        try {
            //将文件保存到指定的目录
            //String url = UploadUtil.upload(file, UploadUtil.PATH+"/upload");
            //使用七牛云图片服务器
            String url = UploadUtil.uploadQiNiuYun(file);

            map.put("status",1);
            map.put("url",UploadUtil.QINIU_PATH+url);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status",0);
            map.put("msg","上传失败");
        }
        return map;
    }

    /**
     * CKEditor 富文本的图片上传格式
     * @param upload : 图片参数名
     * @return
     */
    @RequestMapping("/images1")
    @ResponseBody
    public Map upload1(MultipartFile upload){
        Map<String,Object> map=new HashMap<>();
        try {
            //将文件保存到指定的目录
            //String url = UploadUtil.upload(upload, UploadUtil.PATH+"/upload");
            //使用七牛云图片服务器
            String url = UploadUtil.uploadQiNiuYun(upload);

            map.put("uploaded",1);
            map.put("url",UploadUtil.QINIU_PATH+url);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("uploaded",0);
            HashMap temp=new HashMap();
            temp.put("message","上传失败");
            map.put("error",temp);
        }
        return map;
    }
}
